const SearchAlgorithms = require("../template")

function testOccurrenceSearch() {
  const searcher = new SearchAlgorithms()

  console.log(
    "1. 查找第一个出现位置测试:",
    (() => {
      const firstOccurrenceArray = [1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5]
      const firstTestTargets = [1, 2, 3, 4, 5]
      return firstTestTargets.every(target => {
        const result = searcher.findFirstOccurrence([...firstOccurrenceArray], target)
        const expected = firstOccurrenceArray.indexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "2. 查找最后一个出现位置测试:",
    (() => {
      const lastOccurrenceArray = [1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5]
      const lastTestTargets = [1, 2, 3, 4, 5]
      return lastTestTargets.every(target => {
        const result = searcher.findLastOccurrence([...lastOccurrenceArray], target)
        const expected = lastOccurrenceArray.lastIndexOf(target)
        return result === expected
      })
    })()
  )

  console.log(
    "3. 查找所有出现位置测试:",
    (() => {
      const allOccurrenceArray = [1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5]
      const allTestTargets = [1, 2, 3, 4, 5]
      return allTestTargets.every(target => {
        const result = searcher.findAllOccurrences([...allOccurrenceArray], target)
        const expected = []
        for (let i = 0; i < allOccurrenceArray.length; i++) {
          if (allOccurrenceArray[i] === target) {
            expected.push(i)
          }
        }
        return JSON.stringify(result) === JSON.stringify(expected)
      })
    })()
  )

  console.log(
    "4. 不存在的元素测试:",
    (() => {
      const testArray = [1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5]
      return searcher.findFirstOccurrence([...testArray], 6) === -1 &&
             searcher.findLastOccurrence([...testArray], 6) === -1 &&
             JSON.stringify(searcher.findAllOccurrences([...testArray], 6)) === JSON.stringify([])
    })()
  )

  console.log(
    "5. 全相同元素数组测试:",
    (() => {
      const edgeArray = [5, 5, 5, 5, 5]
      return searcher.findFirstOccurrence([...edgeArray], 5) === 0 &&
             searcher.findLastOccurrence([...edgeArray], 5) === 4 &&
             JSON.stringify(searcher.findAllOccurrences([...edgeArray], 5)) === JSON.stringify([0, 1, 2, 3, 4])
    })()
  )

  console.log(
    "6. 空数组测试:",
    (() => {
      const emptyArray = []
      return searcher.findFirstOccurrence([...emptyArray], 5) === -1 &&
             searcher.findLastOccurrence([...emptyArray], 5) === -1 &&
             JSON.stringify(searcher.findAllOccurrences([...emptyArray], 5)) === JSON.stringify([])
    })()
  )

  console.log(
    "7. 单元素数组测试:",
    (() => {
      const singleArray = [42]
      return searcher.findFirstOccurrence([...singleArray], 42) === 0 &&
             searcher.findLastOccurrence([...singleArray], 42) === 0 &&
             JSON.stringify(searcher.findAllOccurrences([...singleArray], 42)) === JSON.stringify([0])
    })()
  )

  console.log(
    "8. this上下文出现位置搜索测试:",
    (() => {
      const occurrenceObj = {
        multiplier: 2,
        processOccurrenceSearch: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.findFirstOccurrence(arr, target)
          return result * this.multiplier
        }
      }
      return occurrenceObj.processOccurrenceSearch([1, 2, 2, 3], 2) === 2 // 1 * 2 = 2
    })()
  )

  console.log(
    "9. 复杂对象this绑定出现位置搜索测试:",
    (() => {
      const occurrenceConfigObj = {
        config: { offset: 5 },
        processWithConfig: function(arr, target) {
          const searcher = new SearchAlgorithms()
          const result = searcher.findLastOccurrence(arr, target)
          return result + this.config.offset
        }
      }
      return occurrenceConfigObj.processWithConfig([1, 2, 2, 3], 2) === 7 // 2 + 5 = 7
    })()
  )
}

testOccurrenceSearch()
